package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.search.domain.DestinationTemplate;
import cn.wolfcode.luowowo.search.domain.StrategyTemplate;
import cn.wolfcode.luowowo.search.domain.TravelTemplate;
import cn.wolfcode.luowowo.search.domain.UserInfoTemplate;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 首页控制器
 * 接受到 /请求通过这里来进行首页跳转,
 */
@Controller
public class IndexController {
    @Reference
    private IUserInfoRedisService userInfoRedisService;
    @Reference
    private ITravelCommendService travelCommendService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationTemplateSearchService destinationTemplateSearchService;
    @Reference
    private IStrategyTemplateSearchService strategyTemplateSearchService;
    @Reference
    private ITravelTemplateSearchService travelTemplateSearchService;
    @Reference
    private IUserInfoTemplateSearchService userInfoTemplateSearchService;
    @Reference
    private ISearchService searchService;
    /*  String token = CookieUtil.getToken(request, response);这功能在拦截器已经实现了
           UserInfo user = userInfoRedisService.getUserByToken(token);
           request.getSession().setAttribute("userInfo", user);*/
    @RequestMapping("")
    public String index(Model model) {
        //tcs 1.五篇游记的推荐
        model.addAttribute("tcs", travelCommendService.getTop5());
        //sc 2.一篇游记的推荐
        model.addAttribute("sc", strategyCommendService.getTop5().get(0));
        return "index/index";
    }

    //首页游记列表的拼接
    @RequestMapping("/index/travelPage")
    public String travelPage(Model model, @ModelAttribute("qo") TravelQuery qo) {
        model.addAttribute("pageInfo", travelService.query(qo));
        return "index/travelPageTpl";
    }

    //搜索
    @RequestMapping("/q")
    public String search(Model model, @ModelAttribute("qo") SearchQueryObject qo) {
        switch (qo.getType()) {
            case SearchQueryObject.SEARCH_TYPE_DEST:
                return searchDest(model, qo);
            case SearchQueryObject.SEARCH_TYPE_STRATEGY:
                return searchStrategy(model, qo);
            case SearchQueryObject.SEARCH_TYPE_TRAVEL:
                return searchTravel(model, qo);
            case SearchQueryObject.SEARCH_TYPE_USER:
                return searchUser(model, qo);
            default:

                return searchAll(model, qo);
        }
    }

    //查目的地
    private String searchDest(Model model, SearchQueryObject qo) {
        //判断当前关键字是不是一个目的地
        DestinationTemplate dest = destinationTemplateSearchService.findByName(qo.getKeyword());
        //如果是,查询该目的地下的所有攻略,游记,用户
        if (dest != null) {
            SearchResultVO resultVO = new SearchResultVO();
            List<StrategyTemplate> strategyTemplates = strategyTemplateSearchService.findByDestName(dest.getName());
            resultVO.setTotal(strategyTemplates.size()+resultVO.getTotal());
            if (strategyTemplates.size() > 5) {
                strategyTemplates = strategyTemplates.subList(0, 5);
            }
            List<TravelTemplate> travelTemplates = travelTemplateSearchService.findByDestName(dest.getName());
            resultVO.setTotal(travelTemplates.size()+resultVO.getTotal());
            if(travelTemplates.size()>5){
                travelTemplates=travelTemplates.subList(0,5);
            }
            List<UserInfoTemplate> userInfoTemplates = userInfoTemplateSearchService.findByDestName(dest.getName());
            resultVO.setTotal(userInfoTemplates.size()+resultVO.getTotal());
            if(userInfoTemplates.size()>5){
                userInfoTemplates=userInfoTemplates.subList(0,5);
            }
            resultVO.setStrategys(strategyTemplates);
            resultVO.setTravels(travelTemplates);
            resultVO.setUsers(userInfoTemplates);
            model.addAttribute("data",resultVO);
        }
        //如果不是,提示查不到
        model.addAttribute("dest",dest);
        return "index/searchDest";
    }

    //查所有
    private String searchAll(Model model, SearchQueryObject qo) {
        SearchResultVO resultVO = new SearchResultVO();
        Page<DestinationTemplate> ds = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME, DestinationTemplate.class, qo, "name", "info");
        Page<UserInfoTemplate> us = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname", "destName");
        Page<TravelTemplate> ts = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title", "summary");
        Page<StrategyTemplate> ss = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title", "subTitle","summary");
        resultVO.setUsers(us.getContent());
        resultVO.setTravels(ts.getContent());
        resultVO.setStrategys(ss.getContent());
        resultVO.setTotal(resultVO.getTotal()+ds.getTotalElements()+us.getTotalElements()+ts.getTotalElements()+ss.getTotalElements());
        model.addAttribute("data",resultVO);
        return "index/searchAll";
    }

    //查用户
    private String searchUser(Model model, SearchQueryObject qo) {
        Page<UserInfoTemplate> us = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname", "destName");
        model.addAttribute("page",us);
        return "index/searchUser";
    }

    //查游记
    private String searchTravel(Model model, SearchQueryObject qo) {
        Page<TravelTemplate> ts = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title", "summary");
        model.addAttribute("page",ts);return "index/searchTravel";
    }

    //查攻略
    private String searchStrategy(Model model, SearchQueryObject qo) {
        Page<StrategyTemplate> ss = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title", "subTitle","summary");
        model.addAttribute("page",ss);return "index/searchStrategy";
    }
}
